import store from '@/store'
import { useEnvStore } from '@/store/env'
import { createI18n } from 'vue-i18n'
import zh_CN from './zh_CN'
import en_GB from './en_GB'
import ImageResources from './ImageResources'
import netconfig from '@/config/netconfig' //版本号配置引入
import { checkApptext } from '@/api/i18n'

const envStore = useEnvStore(store)

// 检测版本更新i18
export async function checkVersion() {
  const res = await checkApptext(netconfig.i18nVerson)
  let newMessage
  let messageData = res.data
  try {
    if (res.data) {
      newMessage = {
        zh_CN: {
          ...ImageResources,
          ...(messageData ? messageData : {}),
        },
        en: {
          ...ImageResources,
          ...(messageData ? messageData : {}),
        },
      }
    } else {
      newMessage = {
        zh_CN,
        en: en_GB,
      }
    }
  } catch (e) {
    console.error('版本检测接口错误', e)
    newMessage = {
      zh_CN,
      en: en_GB,
    }
  }

  return newMessage
}

let i18n = createI18n({
  locale: envStore.language,
  messages: {
    zh_CN,
    en: en_GB,
  },
  fallbackLocale: 'zh_CN',
  silentFallbackWarn: true,
})
// checkVersion()
export default i18n
